/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddFoodDialog.java
 *
 * Created on 2009-12-6, 10:19:37
 */
package com.ordering.menu;

import com.ordering.dao.TFoodTypeJpaController;
import com.ordering.pojo.TFoodType;
import com.ordering.pojo.TMenu;
import com.ordering.utils.WindowLocationUtils;
import java.awt.Image;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author sulan
 */
public class AddFoodTypeDialog extends javax.swing.JDialog {

    private JTree jTree1;
    javax.swing.JScrollPane jScrollPane1;
    private String photoPath;

    /** Creates new form AddFoodDialog */
    public AddFoodTypeDialog(java.awt.Frame parent, boolean modal, javax.swing.JScrollPane jScrollPane1, JTree jTree1) {
        super(parent, modal);
        
        this.jScrollPane1 = jScrollPane1;
        this.jTree1 = jTree1;
        initComponents();

        WindowLocationUtils.setWindowCenter(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        tFoodType1 = new com.ordering.pojo.TFoodType();
        jLabel1 = new javax.swing.JLabel();
        foodTypeText = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        photoLabel = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("添加菜");
        setAlwaysOnTop(true);
        setModal(true);

        jLabel1.setText("种类:");
        jLabel1.setName("jLabel1"); // NOI18N

        foodTypeText.setName("foodTypeText"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tFoodType1, org.jdesktop.beansbinding.ELProperty.create("${foodTypeName}"), foodTypeText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        foodTypeText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                foodTypeTextActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("宋体", 1, 18));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("添 加 种 类");
        jLabel5.setName("jLabel5"); // NOI18N

        photoLabel.setText("请 上 传 图 片");
        photoLabel.setInheritsPopupMenu(false);
        photoLabel.setName("photoLabel"); // NOI18N
        photoLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                photoLabelMouseClicked(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("宋体", 0, 8));
        jButton1.setText("上传");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("添加");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(118, 118, 118)
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                        .addGap(108, 108, 108))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(foodTypeText, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(172, 172, 172)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(photoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 16, Short.MAX_VALUE)
                    .addComponent(foodTypeText))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(photoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(65, 65, 65)
                .addComponent(jButton2)
                .addGap(30, 30, 30))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void foodTypeTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_foodTypeTextActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_foodTypeTextActionPerformed

    private void photoLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_photoLabelMouseClicked
        // TODO add your handling code here:
}//GEN-LAST:event_photoLabelMouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
//        FileInputStream fis = null;
        try {
            TFoodTypeJpaController jpa = new TFoodTypeJpaController();
            if (photoPath != null && !photoPath.trim().equals("")) {
                File photoFile = new File(photoPath);
                String extName = photoFile.getName().substring(photoFile.getName().indexOf("."));
                tFoodType1.setPhotoName(tFoodType1.getFoodTypeName()+extName);
                WindowLocationUtils.copyFile(photoPath, "pic/" + tFoodType1.getPhotoName());
            } else {
                tFoodType1.setPhotoName(null);
            }

            jpa.create(tFoodType1);

            DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
            DefaultMutableTreeNode menuNodeNew = new DefaultMutableTreeNode(tFoodType1);
            node.add(menuNodeNew);
            this.refreshTree();
            jTree1.expandPath(new TreePath(((DefaultMutableTreeNode) node).getPath()));
            jScrollPane1.repaint();
            
            this.dispose();
        } catch (Exception ex) {
            Logger.getLogger(AddFoodDialog.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        int val = fileChooser.showOpenDialog(AddFoodTypeDialog.this);
        if (val == JFileChooser.APPROVE_OPTION) {
            photoPath = fileChooser.getCurrentDirectory().toString() + "\\" + fileChooser.getSelectedFile().getName();
            ImageIcon ii = new ImageIcon(photoPath);
            ii.setImage(ii.getImage().getScaledInstance(160, 80, Image.SCALE_DEFAULT));

            photoLabel.setIcon(ii);
            photoLabel.repaint();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void refreshTree() {
        DefaultMutableTreeNode rootNode;
        DefaultTreeModel tm;
        //tree
        rootNode = new DefaultMutableTreeNode("菜谱");
        tm = new DefaultTreeModel(rootNode);
        jTree1.setModel(tm);

        TFoodTypeJpaController jpa = new TFoodTypeJpaController();
        List<TFoodType> tfoodTypeList = jpa.findTFoodTypeEntities();
        for (TFoodType fd : tfoodTypeList) {
            DefaultMutableTreeNode foodTypeNode = new DefaultMutableTreeNode(fd);
            rootNode.add(foodTypeNode);

            for (TMenu menu : fd.getTMenuCollection()) {
                DefaultMutableTreeNode menuNode = new DefaultMutableTreeNode(menu);
                foodTypeNode.add(menuNode);
            }
        }
        jTree1.expandPath(new TreePath(((DefaultMutableTreeNode) rootNode).getPath()));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField foodTypeText;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel photoLabel;
    private com.ordering.pojo.TFoodType tFoodType1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
